import axios from 'axios'
import Vue from 'vue'
axios.defaults.baseURL = 'http://www.520gongfu.com:9527/api'
// 将 表单格式数据 转换成对象 或者 将对象格式数据 转换成表单格式
import qs from 'qs'
import { Toast } from 'vant';
Vue.use(Toast);
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers['Authorization'] = token
  }
  // 在发送请求前做些什么
  return config;
}), function (error) {
  // 有错误返回错误
  return Promise.reject(error)
}

export const getSliders = (callback) => {
  axios.get('/home/sliders').then(function (response) {
    callback && callback(response.data)
  })
    .catch(function (error) {
      Toast(error);
    });
}

export const homeCategory = (callback) => {
  axios.get('/home/category').then(function (response) {
    callback && callback(response.data)
  })
    .catch(function (error) {
      Toast(error);
    });
}

export const homeFloorlist = (callback) => {
  axios.get('home/floorlist').then(function (res) {
    callback && callback(res.data)
  }).catch(function (error) {
    Toast(error);
  })
}

export const categoryData = (callback) => {
  axios.get('/category/data').then(res => {
    callback && callback(res.data)
  }).catch(error => {
    Toast(error);
  })
}

export const userLogin = (params) => {
  return axios.post('/user/login', qs.stringify(params)).then(res => {
    const { data } = res
    return data
  }).catch(error => {
    Toast(error);
  })
}

export const productList = (params) => {
  return axios.get('/product/list', {
    params
  }).then(res => {
    const { data } = res
    if (data.status == 0) {
      return data.data
    } else {
      Toast(data.msg);
    }
  })
}

export const productDetail = (params) => {
  return axios.get('product/detail', {
    params
  }).then(res => {
    const { data } = res
    if (data.status == 0) {
      return data.data
    } else {
      Toast(data.msg);
    }
  })
}

export const cartAdd = (params) => {
  return axios.post('/cart/add', qs.stringify(params)).then(res => {
    const { data } = res
    return data
  }).catch(err => {
    Toast(err)
  })
}

// 用户推荐
export const productRecommend = (callback) => {
  axios.get('/product/recommend').then(function (res) {
    callback && callback(res.data)
  }).catch(function (err) {
    Toast(err)
  })
}

// 获取用户信息
export const userInfo = (callback) => {
  axios.get('user/info').then(function (res) {
    callback && callback(res.data)
  }).catch(function (err) {
    console.log(err);
  })
}
// 退出登录
export const userLogout = (params) => {
  return axios.post('user/logout', qs.stringify(params)).then(res => {
    const { data } = res
    return data
  })
}
// 获取购物车列表
export const cartList = (callback) => {
  axios.get('cart/list').then(function (res) {
    callback && callback(res.data)
  }).catch(function (err) {
    console.log(err);
  })
}
// 删除商品
export const cartDel = (params) => {
  return axios.post('cart/del', qs.stringify(params)).then(res => {
    const { data } = res
    return data
  })
}
// 注册
export const userRegister = (params) => {
  return axios.post('user/register', qs.stringify(params)).then(res => {
    const { data } = res
    return data
  })
}
/*
    获取QQ 授权的信息
*/
export const getUserQQinfo = (params, callback) => {
  return axios.get('/user/qqinfo', {
    // get请求参数配置
    params: params, // 参数
    // 序列化参数（格式化处理参数）
    paramsSerializer: function (params) {
      return qs.stringify(params)
    }
  }).then(res => {
    callback && callback(res.data)
  }).catch(err => {
    console.log(err);
  })
}
/*
   获取 QQ 授权信息之后 
   获取 当前 应用的登录授权 token
*/
export const getUserQQlogin = (params, callback) => {
  return axios.post('/user/qqlogin', qs.stringify(params)).then(res => {
    callback && callback(res.data)
  }).catch(err => {
    console.log(err);
  })
}